package jp.ac.waseda.cs.washi.game.factory;

import java.awt.Image;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import jp.ac.waseda.cs.washi.gameaiarena.gui.Renderer;

public class ImageFactory {
	private ImageFactory() {
	}

	public static void prefetchLoad(Renderer render) {
		for (Method method : new ImageFactory().getClass().getMethods()) {
			if (!method.getName().startsWith("load")) {
				continue;
			}
			try {
				method.invoke(null, render);
			} catch (IllegalArgumentException e) {
			} catch (IllegalAccessException e) {
			} catch (InvocationTargetException e) {
			}
		}
	}

	private static Image _title;

	public static Image loadTitle(Renderer render) {
		if (_title == null)
			_title = render.loadImage("img/titleBack/title.png");
		return _title;
	}

	private static Image _barrelBlock;

	public static Image loadBarrelBlock(Renderer render) {
		if (_barrelBlock == null)
			_barrelBlock = render.loadImage("img/etc/taru.png");
		return _barrelBlock;
	}

	private static Image _wall;

	public static Image loadWall(Renderer render) {
		if (_wall == null)
			_wall = render.loadImage("img/bg/wall.png");
		return _wall;
	}

	private static Image _isolatedWall;

	public static Image loadIsolatedWall(Renderer render) {
		if (_isolatedWall == null)
			_isolatedWall = render.loadImage("img/bg/isolatedWall.png");
		return _isolatedWall;
	}

	private static Image _bottomWall;

	public static Image loadBottomWall(Renderer render) {
		if (_bottomWall == null)
			_bottomWall = render.loadImage("img/bg/bottomWall.png");
		return _bottomWall;
	}

	private static Image _underWall;

	public static Image loadUnderWall(Renderer render) {
		if (_underWall == null)
			_underWall = render.loadImage("img/bg/underWall.png");
		return _underWall;
	}

	private static Image _candle;

	public static Image loadCandele(Renderer render) {
		if (_candle == null)
			_candle = render.loadImage("img/etc/candle.png");
		return _candle;
	}

	private static Image _crystal;

	public static Image loadCrystal(Renderer render) {
		if (_crystal == null)
			_crystal = render.loadImage("img/etc/crystal.png");
		return _crystal;
	}

	private static Image[][] _doremiImages;
	private static Image[][] _soraImages;

	public static Image[][] loadCharaImages(final Renderer render, int index) {
		// index==0 : Doremi , index==1 : Sora
		if (index == 0) {
			if (_doremiImages == null) {
				_doremiImages = new Image[][] {
						new Image[] {
								render.loadImage("img/ch/alice_right1.png"),
								render.loadImage("img/ch/alice_right2.png") },
						new Image[] {
								render.loadImage("img/ch/alice_back1.png"),
								render.loadImage("img/ch/alice_back2.png") },
						new Image[] {
								render.loadImage("img/ch/alice_left1.png"),
								render.loadImage("img/ch/alice_left2.png") },
						new Image[] {
								render.loadImage("img/ch/alice_front1.png"),
								render.loadImage("img/ch/alice_front2.png") } };
			}
			return _doremiImages;
		} else {
			if (_soraImages == null) {
				_soraImages = new Image[][] {
						new Image[] {
								render.loadImage("img/ch/rose_right1.png"),
								render.loadImage("img/ch/rose_right2.png") },
						new Image[] {
								render.loadImage("img/ch/rose_back1.png"),
								render.loadImage("img/ch/rose_back2.png") },
						new Image[] {
								render.loadImage("img/ch/rose_left1.png"),
								render.loadImage("img/ch/rose_left2.png") },
						new Image[] {
								render.loadImage("img/ch/rose_front1.png"),
								render.loadImage("img/ch/rose_front2.png") } };
			}
			return _soraImages;
		}
	}

	private static Image _doremiMagicalCircleImage;
	private static Image _soraMagicalCircleImage;

	public static Image loadMagicalCircle(Renderer render, int index) {
		if (index == 0) {
			if (_doremiMagicalCircleImage == null)
				_doremiMagicalCircleImage = render
						.loadImage("img/mc/mahouzin_pink.png");
			return _doremiMagicalCircleImage;
		} else {
			if (_soraMagicalCircleImage == null)
				_soraMagicalCircleImage = render
						.loadImage("img/mc/mahouzin_green.png");
			return _soraMagicalCircleImage;

		}
	}

	private static Image _doremiTerritoryImage;
	private static Image _soraTerritoryImage;

	public static Image loadTerritory(Renderer render, int index) {
		if (index == 0) {
			if (_doremiTerritoryImage == null)
				_doremiTerritoryImage = render
						.loadImage("img/tile/mahouzin_a4.png");
			return _doremiTerritoryImage;
		} else {
			if (_soraTerritoryImage == null)
				_soraTerritoryImage = render
						.loadImage("img/tile/mahouzin_r4.png");
			return _soraTerritoryImage;
		}
	}

	private static Image[] _doremiOwnerdCandleImage;
	private static Image[] _soraOwnerdCandleImage;

	public static Image[] loadOwnerdCandle(Renderer render, int index) {
		if (index == 0) {
			if (_doremiOwnerdCandleImage == null) {
				_doremiOwnerdCandleImage = new Image[] {
						render.loadImage("img/etc/doremi_candle.png"),
						render.loadImage("img/etc/doremi_crystal.png"), };
			}
			return _doremiOwnerdCandleImage;
		} else {
			if (_soraOwnerdCandleImage == null) {
				_soraOwnerdCandleImage = new Image[] {
						render.loadImage("img/etc/sora_candle.png"),
						render.loadImage("img/etc/sora_crystal.png"), };
			}
			return _soraOwnerdCandleImage;
		}
	}

	private static Image _doremiBonusImages;
	private static Image _soraBonusImages;
	private static Image _doremiBigBonusImages;
	private static Image _soraBigBonusImages;

	public static Image[][] loadPumpkinImages(Renderer render) {
		if (_doremiBonusImages == null) {
			_doremiBonusImages = render.loadImage("img/tile/pump_a.png");
		}
		if (_soraBonusImages == null) {
			_soraBonusImages = render.loadImage("img/tile/pump_r.png");
		}
		if (_doremiBigBonusImages == null) {
			_doremiBigBonusImages = render.loadImage("img/tile/pump_a2.png");
		}
		if (_soraBigBonusImages == null) {
			_soraBigBonusImages = render.loadImage("img/tile/pump_r2.png");
		}

		return new Image[][] {
				new Image[] { _doremiBonusImages, _doremiBigBonusImages },
				new Image[] { _soraBonusImages, _soraBigBonusImages }, };
	}

	private static Image[] _doremiScoreImages;
	private static Image[] _soraScoreImages;

	public static Image[][] loadScoreImages(Renderer render) {
		if (_doremiScoreImages == null) {
			_doremiScoreImages = new Image[] {
					render.loadImage("img/titleBack/a0.png"),
					render.loadImage("img/titleBack/a1.png"),
					render.loadImage("img/titleBack/a2.png"),
					render.loadImage("img/titleBack/a3.png"),
					render.loadImage("img/titleBack/a4.png"),
					render.loadImage("img/titleBack/a5.png"),
					render.loadImage("img/titleBack/a6.png"),
					render.loadImage("img/titleBack/a7.png"),
					render.loadImage("img/titleBack/a8.png"),
					render.loadImage("img/titleBack/a9.png"),
					render.loadImage("img/titleBack/name_a.png") };
		}
		if (_soraScoreImages == null) {
			_soraScoreImages = new Image[] {
					render.loadImage("img/titleBack/r0.png"),
					render.loadImage("img/titleBack/r1.png"),
					render.loadImage("img/titleBack/r2.png"),
					render.loadImage("img/titleBack/r3.png"),
					render.loadImage("img/titleBack/r4.png"),
					render.loadImage("img/titleBack/r5.png"),
					render.loadImage("img/titleBack/r6.png"),
					render.loadImage("img/titleBack/r7.png"),
					render.loadImage("img/titleBack/r8.png"),
					render.loadImage("img/titleBack/r9.png"),
					render.loadImage("img/titleBack/name_r.png") };
		}
		return new Image[][] { _doremiScoreImages, _soraScoreImages };
	}

	private static Image[] _aliceTileImages;
	private static Image[] _roseTileImages;
	private static Image[] _defaultTileImages;

	// タイル強度の最大値（この分だけタイルの数字画像を用意する）
	private static int MAX_LEVEL = 10;

	public static Image[][] loadTileImages(Renderer render) {
		// 数字に対応するイメージをセットする。
		String basePath = "img/tile/";

		if (_aliceTileImages == null) {
			_aliceTileImages = loadPlayerTileImages(render, basePath, "a");
		}
		if (_roseTileImages == null) {
			_roseTileImages = loadPlayerTileImages(render, basePath, "r");
		}
		if (_defaultTileImages == null) {
			_defaultTileImages = new Image[] { render
					.loadImage("img/tile/tile.png") };
		}
		return new Image[][] { _aliceTileImages, _roseTileImages,
				_defaultTileImages };
	}

	private static Image[] loadPlayerTileImages(Renderer render,
			String basePath, String prefix) {
		Image[] images = new Image[MAX_LEVEL];
		for (int i = 1; i < images.length; i++) {
			String path = basePath + prefix + i + ".png";
			images[i] = render.loadImage(path);
		}
		return images;
	}

	private static Image[] _timeImages;

	public static Image[] loadTimeImages(Renderer render) {
		if (_timeImages == null) {
			_timeImages = new Image[] {
					render.loadImage("img/titleBack/t0.png"),
					render.loadImage("img/titleBack/t1.png"),
					render.loadImage("img/titleBack/t2.png"),
					render.loadImage("img/titleBack/t3.png"),
					render.loadImage("img/titleBack/t4.png"),
					render.loadImage("img/titleBack/t5.png"),
					render.loadImage("img/titleBack/t6.png"),
					render.loadImage("img/titleBack/t7.png"),
					render.loadImage("img/titleBack/t8.png"),
					render.loadImage("img/titleBack/t9.png"),
					render.loadImage("img/titleBack/time.png"),
					render.loadImage("img/titleBack/t_colon.png") };
		}
		return _timeImages;
	}

	private static Image _scoreBoard;

	public static Image loadScoreBoardImage(Renderer render) {
		if (_scoreBoard == null)
			_scoreBoard = render.loadImage("img/titleBack/scoreBoard.png");
		return _scoreBoard;
	}
}
